package com.example.datacontact;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityAdd extends Activity {
	SqlHandler sqlHandler;
	EditText edtName, edtPhone;
	Button btnInsert;
	Button btnCannel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_add);
		
		
		
		edtName = (EditText) findViewById(R.id.edtInsertName);
		edtPhone = (EditText) findViewById(R.id.edtInsertPhone);
		btnInsert=(Button)findViewById(R.id.btnInsert);
		btnCannel=(Button)findViewById(R.id.btnCannel);
		
		btnCannel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				back();
			}
		});
		sqlHandler = new SqlHandler(this);
		btnInsert.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name = edtName.getText().toString();
				String phoneNo = edtPhone.getText().toString();
				if(name.length()>0 && phoneNo.length()>0)
				{
					String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('"
							+ name + "','" + phoneNo + "')";
					sqlHandler.executeQuery(query);
					edtName.setText("");
					edtPhone.setText("");
				}
				else
				{
					AlertDialog.Builder alertBuilder=new AlertDialog.Builder(ActivityAdd.this);
					alertBuilder.setTitle("Invalid Data");
					alertBuilder.setMessage("Please, Enter valid data");
					alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
					});
					alertBuilder.create().show();
				}
			}
		});	
	}
	private void back()
	{
		this.onBackPressed(); //Hàm gọi nút back trên màn hình
	}
}
